function Promise(executor) {
    this.status = 'pending';
    this.value = null;
    this.reason = null;
    this.onFulfilledArray = [];
    this.onRejectedArray = [];
    
    const resolve = value => {
        if (value instanceof Promise) {
            return value.then(resolve, reject);
        }

        setTimeout(() => {
            if (this.status === 'pending') {
                this.value = value;
                this.status = 'fulfilled';

                this.onFulfilledArray.forEach(func => {
                    func(value);
                })
            }
        })
    }

    const reject = reason => {
        setTimeout(() => {
            if (this.status === 'pending') {
                this.reason = reason;
                this.status = 'rejected';

                this.onRejectedArray.forEach(func => {
                    func(reason);
                })
            }
        })
    }

    try {
        executor(resolve, reject);
    } catch (e) {
        reject(e);
    }
}

Promise.prototype.then = function(onfulfilled, onrejected) {
    onfulfilled = typeof onfulfilled === 'function' ? onfulfilled : data => data;
    onrejected = typeof onrejected === 'function' ? onrejected : error => { throw error };
    
    // promise2 将作为 then 方法的返回值
    let promise2;

    if (this.status === 'fulfilled') {
        return promise2 = new Promise((resolve, reject) => {
            setTimeout(() => {
                try {
                    // 这个新的 promse2 resolved 的值为 onfulfilled 的执行结果
                    let result = onfulfilled(this.value);
                    resolve(result);
                } 
                catch(e) {
                    reject(e);
                }
            })
        })
    }

    if (this.status === 'rejected') {
        return promise2 = new Promise((resolve, reject) => {
            setTimeout(() => {
                try {
                    // 这个新的 promise2 reject 的值为 onrejected 的执行结果
                    let result = onrejected(this.reason);
                    resolve(result);
                }
                catch(e) {
                    reject(e);
                }
            }) 
        })
    }

    if (this.status === 'pending') {
        return promise2 = new Promise((resolve, reject) => {
            this.onFulfilledArray.push(() => {
                try {
                    let result = onfulfilled(this.value);
                    resolve(result);
                }
                catch(e) {
                    reject(e);
                }
            })

            this.onRejectedArray.push(() => {
                try {
                    let result = onrejected(this.reason);
                    resolve(result);
                }
                catch(e) {
                    reject(e);
                }
            })
        })
    }
}



const promise = new Promise((resolve, reject) => {
    setTimeout(() => {
        resolve('lucas')
    }, 2000)
  })
  
  promise.then(data => {
    console.log(data)
    return new Promise((resolve, reject) => {
      setTimeout(() => {
          resolve(`${data} next then`)
      }, 4000)
    })
  })
  .then(data => {
    console.log(data)
  })